package com.woniu.rear.comm.web.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.woniu.rear.comm.core.code.FileUploadErrorEnum;
import com.woniu.rear.comm.web.config.OssConfig;
import com.woniu.rear.comm.web.exception.RearFileUploadException;

import lombok.extern.slf4j.Slf4j;

/**
 * OSS模板类
 * @author liwei
 *
 */
@Slf4j
public class OSSTemplate {

	/*参数对象*/
	private OssConfig config;
	
	public OSSTemplate(OssConfig config) {
    	this.config = config;
    }
	
	/**
	 * OSS上传文件
	 * @param bucketName
	 * @param path 文件全路径名
	 * @param file 文件对象
	 * @return 文件访问路径
	 * @throws FileNotFoundException
	 */
	public String uploadFile(String bucketName,String path,File file) throws FileNotFoundException {
		OSS ossClient = null;
		try {
			ossClient = new OSSClientBuilder().build(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
			if(!ossClient.doesBucketExist(bucketName)) {
	             ossClient.createBucket(bucketName);
	             CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
	             createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
	             ossClient.createBucket(createBucketRequest);
	        }
			ossClient.putObject(bucketName,path,file);
			return "http://"+bucketName+"."+config.getEndpoint()+"/"+path;
		} catch (Exception e) {
			log.error("上传失败: ",e);
			throw new RearFileUploadException(FileUploadErrorEnum.UPLOAD_ERROR);
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
	}
	
	/**
	 * OSS上传文件
	 * @param bucketName
	 * @param path 文件全路径名
	 * @param ist 文件对象流
	 * @return
	 */
	public String uploadFile(String bucketName,String path,InputStream ist) {
		OSS ossClient = null;
		try {
			ossClient = new OSSClientBuilder().build(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
			if(!ossClient.doesBucketExist(bucketName)) {
	             ossClient.createBucket(bucketName);
	             CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
	             createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
	             ossClient.createBucket(createBucketRequest);
	        }
			ossClient.putObject(bucketName,path,ist);
			return "http://"+bucketName+"."+config.getEndpoint()+"/"+path;
		} catch (Exception e) {
			log.error("上传失败: ",e);
			throw new RearFileUploadException(FileUploadErrorEnum.UPLOAD_ERROR);
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
		
	}
	
	/**
	 * OSS删除文件
	 * @param bucketName
	 * @param path 文件全路径名
	 */
	public void deleteFile(String bucketName,String path) {
		OSS ossClient = null;
		try {
			ossClient = new OSSClientBuilder().build(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
			ossClient.deleteObject(bucketName, path);
		} catch(Exception e) {
			log.error("删除失败: ",e);
			throw new RearFileUploadException(FileUploadErrorEnum.UPLOAD_DELETE_ERROR);
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
		
	}
	
}






